<?php

/**
 * StaffPubOrganization form.
 *
 * @package    erp-dnict
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class StaffPubOrganizationForm extends BaseStaffPubOrganizationForm
{
  public function configure()
  {
    $this->setWidgets(array(
        'pub_organization_id'           =>  new sfWidgetFormSelect(array('choices' => $this->getAllPubOrganizations())),
        'start_date'                    =>  new sfWidgetFormInput(),
        'position'                      =>  new sfWidgetFormInput(),
        ));

    $this->widgetSchema->setLabels(array(
        'pub_organization_id'           =>  'Tổ chức:',
        'start_date'                    =>  'Ngày bắt đầu:',
        'position'                      =>  'Chức vụ:',
        ));

    $this->setValidators(array(
        'pub_organization_id'           => new sfValidatorChoice(array('choices' => array_keys($this->getAllPubOrganizations()), 'required' => 'true')),
        'start_date'                    => new sfValidatorDate(array('required' => false, 'date_output' => 'dd/mm/yy', 'date_format' => "/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/")),
        'position'                      => new sfValidatorString(array('required' => false)),
        ));

    $this->widgetSchema->setNameFormat('staff_pub_organization[%s]');
  }

  private function getAllPubOrganizations()
  {
    $q = Doctrine_Query::create()
            ->from('PubOrganization p')
            ->where('p.is_active = ?', true);

    $results = $q->execute();
    foreach ($results as $result)
        $items[$result['id']] = $result['name'];

    return $items;
  }
}
